#pragma once

#include <unordered_map>

#include "memdb/memdb.h"

namespace memdb {
namespace impl {
class IMeta;
class Factory;
}//namespace impl
}//namespace memdb

namespace memdb {
namespace impl {
namespace simple {


class SimpleTables : public ITables {
public:
		explicit SimpleTables(IMeta & meta, const Factory & factory);
		~SimpleTables() override;

		typedef typename ITables::KeyType KeyType;
		typedef typename ITables::ValueType ValueType;
  
    typedef std::unordered_map<KeyType, ValueType> Map;


public:

	std::pair<std::error_code, ValueType> create(const KeyType & name) override;
	std::error_code drop(const KeyType & name) override;
  bool exists(const KeyType & name) const override;
  std::optional<ValueType> get(const KeyType & name) const override;

  size_t count() const override;


  class Iterator  : public ITables::Iterator {
  public:
    Iterator(SimpleTables * tables) ;
    ~Iterator()override;

    std::optional<ITables::Item> next() override;

  private:
    SimpleTables::Map::iterator _current; 
    const SimpleTables::Map::iterator _end; 
  };

  std::unique_ptr<ITables::Iterator> iterator() const override;

private:
	IMeta & _meta;
	const Factory & _factory;	
	Map mem;
  friend class Iterator;
};

}//namespace simple
}//namespace impl
}//namespace memdb
